package com.holly.common_view.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView分割线
 * 2016/11/17 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class GridLayoutItemDecoration extends RecyclerView.ItemDecoration {
    public static final int DEFAULT_SIZE = 2;
    public static final int DEFAULT_COLOR = Color.parseColor("#C8C8C8");
    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private int mSize;
    private int mColor;
    private final Paint mPaint;
    private int mOrientation;

    public GridLayoutItemDecoration(Context context, int orientation) {
        this(context, orientation, DEFAULT_COLOR, DEFAULT_SIZE);
    }

    public GridLayoutItemDecoration(Context context, int orientation, int color){
        this(context, orientation, color, DEFAULT_SIZE);
    }

    public GridLayoutItemDecoration(Context context, int orientation, int color, int size){
        setOrientation(orientation);
        mPaint = new Paint();
        mColor = color;
        mSize = size;
        mPaint.setColor(mColor);
    }

    public void setColor(@ColorInt int color) {
        mColor = color;
        mPaint.setColor(mColor);
    }

    public void setSize(int size) {
        mSize = size;
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        mPaint.setColor(mColor);
        int dividerSize = mSize;
        int spanCount = getSpanCount(parent);
        final int childCount = parent.getChildCount();
        for (int itemPosition = 0; itemPosition < childCount; itemPosition++) {
            final View child = parent.getChildAt(itemPosition);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin;
            if ((itemPosition + 1) % spanCount == 0) {
                canvas.drawRect(left, bottom, right, bottom + dividerSize, mPaint);
            } else {
                canvas.drawRect(left, bottom, right, bottom + dividerSize, mPaint);
                canvas.drawRect(right, top, right+dividerSize, bottom+dividerSize, mPaint);
            }
        }
    }


    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        mPaint.setColor(mColor);
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin;
            if (i % 2 == 0) {
                canvas.drawRect(left, bottom - mSize, right - mSize, bottom, mPaint);
                canvas.drawRect(right - mSize, top - 1, right, bottom, mPaint);
            } else {
                canvas.drawRect(left, bottom - mSize, right, bottom, mPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int spanCount = getSpanCount(parent);
        if (mOrientation == VERTICAL_LIST) {
            if ((itemPosition + 1) % spanCount == 0) {
                //表示每排右边最后一个
                outRect.set(0, 0, 0, mSize);
            } else {
                //表示每排右边最后一个
                outRect.set(0, 0, mSize, mSize);
            }
        } else {
            if ((itemPosition + 1) % spanCount == 0) {
                //表示每排右边最后一个
                outRect.set(0, 0, mSize, 0);
            } else {
                //表示每排右边最后一个
                outRect.set(0, 0, mSize, mSize);
            }
        }
    }

    private int getSpanCount(RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (!(layoutManager instanceof GridLayoutManager)) {
            throw new IllegalArgumentException("只有GridLayoutManager才可以使用该Divider");
        }
        GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        return gridLayoutManager.getSpanCount();
    }
}
